﻿/*
 *  armour.js
 *
 *  jQuery plugin to handle the creation, update and removal of armour and equipment records.
 */
(function ($) {
    var settings = {
        updateArmourItemUrl: '',                    // service url of update armour ajax call.
        updateArmourCallback: null,                 // call back function after armour has been updated.
        removeArmourLinkSelector: '.remove-item',   // selector for all remove armour item links.
        removeArmourCallback: null,                 // after armour removed event call back function. parameter link = link clicked.
        armourListUrl: '',                                  // service url to refresh armour list.
        armourListContainerSelector: '.armour-container',   // selector for armour list container.
        refreshArmourCallback: null,                        // call back function after armour list refreshed.
        equipLinkSelector: '.equip-item',                   // selector for all equip/un-equip links.
        equipItemCallback: null                             // call back function called after item equipped/un-equipped.
    };

    var busy = false;

    var methods = {
        init: function (options) {
            $('.message-box').hide();

            settings = $.extend(settings, options);

            var $this = $(this);

            bindUpdateArmourEvents();

            bindRemoveArmourEvent($this);

            bindEquipArmourEvent($this);
        },
        refreshArmourList: function () {
            _refreshArmourList();
        }
    };


    $.fn.armour = function (method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on armour object.');
        }

    };

    /*
    *  Refresh the armour list html.
    */
    function _refreshArmourList() {
        doAjaxGet(
                settings.armourListUrl,
                settings.armourListContainerSelector,
                function () {

                    bindUpdateArmourEvents();

                    bindRemoveArmourEvent();

                    bindEquipArmourEvent();

                    if (settings.refreshArmourCallback)
                        settings.refreshArmourCallback();
                },
                showWarning);
    }

    /*
    *  Bind events for the remove armour anchor.
    */
    function bindRemoveArmourEvent() {
        $(settings.removeArmourLinkSelector).click(function () {
            event.preventDefault();

            var $this = $(this);

            if (busy)
                return;

            busy = true;

            var url = $this.attr('href');

            doAjaxGet(url, null,
                function () {
                    if (settings.removeArmourCallback)
                        settings.removeArmourCallback($this);

                    showMessage('Armour removed.');

                    busy = false;
                },
                function (data) {
                    showErrorText('Failed to remove armour.');
                    showWarning(data);
                    busy = false;
                });
        });
    }


    function bindUpdateArmourEvents() {
        if (!settings.updateArmourItemUrl)
            return;

        // Handle the add adventure record item form submit.
        $('#editArmourForm').submit(function (event) {
            event.preventDefault();

            if (busy)
                return;

            busy = true;

            var $this = $(this);

            var itemId = $('#itemId').val();
            var armourType = $('#armourType').val();
            var itemName = $('#itemName').val();
            var itemSuperior = $('#itemSuperior').val();
            var hqId = $('#itemHQID').val();
            var itemEmpowered = $('#itemEmpowered').val();
            var itemEnsorcelled = $('#itemEnsorcelled').val();

            // validate date and number values.
            var formValid = true;

            if (armourType != 'Metal' && armourType != 'NonMetal' && armourType != 'Tabard' && armourType != 'Headwear')
                formValid = false;

            if (!formValid) {
                showMessage('The form is not valid.');
                busy = false;
                return;
            }

            var url = settings.updateArmourItemUrl;

            url += '?type=' + armourType + '&name=' + itemName + '&hqid=' + hqId + '&superior=' + itemSuperior + '&empowered=' + itemEmpowered + '&ensorcelled=' + itemEnsorcelled;

            doAjaxGet(url, null,
                function () {
                    if (settings.updateArmourCallback)
                        settings.updateArmourCallback();

                    showMessage('Updated armour named ' + itemName + '.');

                    busy = false;
                },
                function (data) {
                    showMessage('Failed to update armour named ' + itemName + '.');
                    showWarning(data);
                    busy = false;
                });
        });
    }

    /*
    *  Bind link event handlers for the equip/un-equip action.
    */
    function bindEquipArmourEvent() {
        $(settings.equipLinkSelector).click(function (event) {
            event.preventDefault();

            if (busy)
                return;

            busy = true;

            var $this = $(this);
            var equipped = $this.attr('data-equipped');

            equipped = (equipped == "true") ? "false" : "true";

            var url = $this.attr('href');
            url += '?equipped=' + equipped;

            doAjaxGet(url, null,
                function () {
                    if (settings.equipItemCallback)
                        settings.equipItemCallback();

                    if (equipped)
                        showMessage('Armour equipped');
                    else
                        showMessage('Armour un-equipped');

                    busy = false;
                },
                function (data) {
                    if (equipped)
                        showErrorText('Failed to equip armour.');
                    else
                        showErrorText('Failed to un-equip armour.');

                    showWarning(data);
                    busy = false;
                });
        });
    }

})(jQuery);